﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Service;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Web.DbHelper;
using System.IO;

namespace KnowledgeCommunity.Web.Controllers
{
    public class ZoneController : Controller
    {
        DocumentHelper myDocumentHelper = new DocumentHelper();
        KnowledgeHelper myKnowledgeHelper = new KnowledgeHelper();
        CommodityHelper myCommodityHelper = new CommodityHelper();
        TribuneHelper myTribuneHelper = new TribuneHelper();
        TroubleHelper myTroubleHelper = new TroubleHelper();
        AnswerHelper myAnswerHelper = new AnswerHelper();
        TribuneTopicHelper myTribuneTopicHelper = new TribuneTopicHelper();
        ShopHelper myShopHelper = new ShopHelper();

        TopicHelper myTopicHelper = new TopicHelper();
        UserAccountHelper myUserAccountHelper = new UserAccountHelper();

        //
        // GET: /Zone/

        public ActionResult Index()
        {
            string user=HttpContext.User.Identity.Name;
            int k1 = myDocumentHelper.UserDocumentCount(user);
            int n1 = myDocumentHelper.AllDocumentCount();

            int k2 = myKnowledgeHelper.UserKnowledgeCount(user);
            int n2 = myKnowledgeHelper.AllKnowledgeCount();

            int k3 = myCommodityHelper.UserCommodityCount(user);
            int n3 = myCommodityHelper.AllCommodityCount();

            int k4 = myTribuneHelper.UserTribuneCount(user);
            int n4 = myTribuneHelper.AllTribuneCount();

            int k5 = myTribuneTopicHelper.UserTribuneTopicCount(user);
            int n5 = myTribuneTopicHelper.AllTribuneTopicCount();

            int k6 = myAnswerHelper.UserAnswerCount(user);
            int n6 = myAnswerHelper.AllAnswerCount();

            double item1 = 0.1;
            double item2 = 0.1;
            double item3 = 0.1;

            if (n2 != 0 && n6 != 0)
            {
                float a1 = k2 / n2;
                float a2 = k6 / n6;
                item1 = (0.7) * a1 + (0.3) * a2;
            }
            if (n3 != 0 )
            {
                item2 = k3 / n3;
            }
            
            if (n1 != 0 && n4 != 0 && n5 != 0)
            {
                float a1 = k1 / n1;
                float a2 = k4 / n4;
                float a3 = k5 / n5;
                item3 = (0.5) * a1 + (0.3) * a2 + (0.2) * a3;
            }
            ViewBag.Bar1 = item1;
            ViewBag.Bar2 = item2;
            ViewBag.Bar3 = item3;



            ViewData["TopKno"] = myKnowledgeHelper.FindHotKnowledge(6);
            ViewData["TopDoc"] = myDocumentHelper.FindHotDocument(6);
            ViewData["TopCom"] = myCommodityHelper.FindTop(6);
            ViewData["TribuneList"]=myTribuneHelper.FindAll();
            ViewData["TopTopic"] = myTopicHelper.FindTop(10);
            ViewData["NewUsers"] = myUserAccountHelper.NewUserAccount(10);

            return View();
        }

        #region  ///Managed

        [Authorize(Roles = "admin,editor")]
        public ActionResult Waiting()
        {
            ViewData["Doc"] = myDocumentHelper.FindByState(0);
            ViewData["Kno"] = myKnowledgeHelper.FindByState(0);
            ViewData["Com"] = myCommodityHelper.FindByState(0);
            ViewData["Tri"] = myTribuneHelper.FindByState(0);

            return View();
        }

        [Authorize(Roles = "admin,editor")]
        public ActionResult Approved()
        {
            ViewData["Doc"] = myDocumentHelper.FindByState(1);
            ViewData["Kno"] = myKnowledgeHelper.FindByState(1);
            ViewData["Com"] = myCommodityHelper.FindByState(1);
            ViewData["Tri"] = myTribuneHelper.FindByState(1);
            return View();
        }

        [Authorize(Roles = "admin,editor")]
        public ActionResult DoApproveK(int id)
        {
            var model = myKnowledgeHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult ApproveK(Knowledge model) 
        {
            model.State = 1;
            UpdateModelKnowledge(model);
            return Waiting();
        }


        [Authorize(Roles = "admin,editor")]
        public ActionResult DoApproveD(int id)
        {
            var model = myDocumentHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult ApproveD(Document model)
        {
            model.State = 1;
            UpdateModelDocument(model);
            return Waiting();
        }

        [Authorize(Roles = "admin,editor")]
        public ActionResult DoApproveC(int id)
        {
            var model = myCommodityHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult ApproveC(Commodity model)
        {
            model.State = 1;
            UpdateModelCommodity(model);
            return Waiting();
        }


        [Authorize(Roles = "admin,editor")]
        public ActionResult DoApproveT(int id)
        {
            var model = myTribuneHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult ApproveT(Tribune model)
        {
            model.State = 1;
            UpdateModelTribune(model);
            return Waiting();
        }

        [Authorize(Roles = "admin,editor")]
        public ActionResult Canceled()
        {
            ViewData["Doc"] = myDocumentHelper.FindByState(2);
            ViewData["Kno"] = myKnowledgeHelper.FindByState(2);
            ViewData["Com"] = myCommodityHelper.FindByState(2);
            ViewData["Tri"] = myTribuneHelper.FindByState(2);
            return View();
        }

      
        [Authorize(Roles = "admin,editor")]
        public ActionResult DoCancelK(int id)
        {
            var model = myKnowledgeHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult CancelK(Knowledge model)
        {
            model.State = 2;
            UpdateModelKnowledge(model);
            return Waiting();
        }


        [Authorize(Roles = "admin,editor")]
        public ActionResult DoCancelD(int id)
        {
            var model = myDocumentHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult CancelD(Document model)
        {
            model.State = 2;
            UpdateModelDocument(model);
            return Waiting();
        }


        [Authorize(Roles = "admin,editor")]
        public ActionResult DoCancelC(int id)
        {
            var model = myCommodityHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult CancelC(Commodity model)
        {
            model.State = 2;
            UpdateModelCommodity(model);
            return Waiting();
        }

        [Authorize(Roles = "admin,editor")]
        public ActionResult DoCancelT(int id)
        {
            var model = myTribuneHelper.FindById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult CancelT(Tribune model)
        {
            model.State = 2;
            UpdateModelTribune(model);
            return Waiting();
        }

        #endregion

        #region  ///UserViews

        public ActionResult MyDocument() 
        {
            string user=HttpContext.User.Identity.Name;
            var model = myDocumentHelper.FindByUserName(user);
            ViewData["myDoc"] = model;
            return View();
        }

        public ActionResult MyKnowledge()
        {
            string user = HttpContext.User.Identity.Name;
            var model = myKnowledgeHelper.FindByUserName(user);
            ViewData["myKno"] = model;
            return View();
        }

        public ActionResult MyCommodity()
        {
            string user = HttpContext.User.Identity.Name;
            var model = myCommodityHelper.FindByUserName(user);
            ViewData["myCom"] = model;
            return View();
        }

        public ActionResult MyTribune() 
        {
            string user = HttpContext.User.Identity.Name;
            var model = myTribuneHelper.FindByUserName(user);
            ViewData["myTri"] = model;
            return View();
        }

        public ActionResult MyTrouble()
        {
            string user = HttpContext.User.Identity.Name;
            var model = myTroubleHelper.FindByUserName(user);
            ViewData["myTro"] = model;
            return View();
        }

        public ActionResult OrderList() 
        {
            string user = HttpContext.User.Identity.Name;
            ViewData["myTro"] = myShopHelper.FindShopByCommodityOwner(user);
            return View();
        }

        public ActionResult MyOrder()
        {
            string user = HttpContext.User.Identity.Name;
            var model = myShopHelper.FindByUserName(user);
            ViewData["myTro"] = model;
            return View();
        }

        public ActionResult OwnerDoApprove(int id) 
        {
            var model = myShopHelper.FindById(id);
            model.State = 1;
            
            var com1=myCommodityHelper.FindById(model.C_id);
            if (com1.Count >= model.Count)
            {
                DoUpdateModel(model);
                com1.Count = com1.Count - model.Count;
                UpdateModelAboutCount(com1);
                return OrderList();
            }
            else 
            {
                string msg="操作失败！你所拥有的商品库存已经不足！请补足库存后操作！";
                return RedirectToAction("Index", "Notice", new { message = msg });
            }
            
        }

        public ActionResult OwnerDoCancel(int id) 
        {
            var model = myShopHelper.FindById(id);
            model.State = 2;
            DoUpdateModel(model);
            return OrderList();
        }

        #endregion

        CKCDB mydb = new CKCDB();

        private void DoUpdateModel(Shop model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Shops.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Shops>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

        private void UpdateModelAboutCount(Commodity model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Commodities.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Commodities>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

        private void UpdateModelKnowledge(Knowledge model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Knowledges.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Knowledges>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

        private void UpdateModelDocument(Document model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Documents.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Documents>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

        private void UpdateModelCommodity(Commodity model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Commodities.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Commodities>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

        private void UpdateModelTribune(Tribune model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var app = mydb.Tribunes.Find(model.Id);
                    UpdateModel(app);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    string message = "Create in table<Tribunes>" + ex.ToString();
                    WriteLog.WriteSysLog(message);
                }
            }
        }

    }
}
